﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Patent.DBUtility;
using Patent.Json;
using System.Net.Json;


public partial class Web_IPC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //getIPC(Request.QueryString["type"]);
        /*
        ptcase_m ptcase = (ptcase_m)Session["topic"];
        string tableName = ptcase.t7;
        getIPC(Request.QueryString["parent"],tableName);
         */
    }
       
    /// <summary>
    /// 格式化传输数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public bool formateDatSet(DataSet data)
    {
        return true;
    }

    /// <summary>
    /// 根据父节点得出子结点
    /// </summary>
    /// <param name="parent">父亲节点</param>
    /// <param name="tableName">表名</param>
    /// <returns>子结点已xml格式</returns>
    public void getIPC(string parent,string tableName)
    {
        SQLHelper helper = new SQLHelper();
        string sql="";
        DataSet data = new DataSet();

   
        try
        {
            if (parent.Length == 1)//大类
            {
                sql = "select 大类 as son,count(大类) as count from " + tableName + " where 部类='" + parent + "' group by 大类";

            }
            if (parent.Length == 3)//小类
            {
                sql = "select 小类 As son,count(小类) as count from " + tableName + " where 部类+大类='" + parent + "' group by 小类";
            }

            if (parent.Length == 4)//主组
            {
                sql = "select 主组 as son, count(主组) as count from " + tableName + " where 部类+大类+小类='" + parent + "' group by 主组";
            }


            if (parent.Length > 4)//分组
            {
                sql = "select 分组 as son, count(分组) as count from " + tableName + " where 部类+大类+小类+主组='" + parent + "' group by 分组";
            }

            if (parent == "root")
            {
                sql = "select 部类 as son,count(部类) as count from " + tableName + " group by 部类 ";
            }
        }
        catch (Exception)
        {
            sql = "select 部类 as son,count(部类) as count from " + tableName + " group by 部类 ";
        }

        helper.RunSQL(sql, ref data);
        //json格式文件

        JsonObjectCollection collection;
        JsonArrayCollection s = new JsonArrayCollection();
        //s.Name = "son";
        
        
        for (int i = 0; i < data.Tables[0].Rows.Count; i++)
        {           
            collection = new JsonObjectCollection();
            collection.Add(new JsonStringValue("id", parent+data.Tables[0].Rows[i]["son"].ToString()));
            collection.Add(new JsonStringValue("text", parent+data.Tables[0].Rows[i]["son"].ToString() + "(" + data.Tables[0].Rows[i]["count"].ToString() + ")"));
            if (data.Tables[0].Rows[i]["son"].ToString().Contains("/"))
            {
                collection.Add(new JsonBooleanValue("leaf", true));
            }
            else
            {
                collection.Add(new JsonBooleanValue("leaf", false));
            }
            collection.Add(new JsonBooleanValue("checked", false));
            s.Insert(s.Count, collection);            
        }

        Response.Write(s);
        Response.End();
        Response.Flush();
      
    }
}
